﻿#include "registerui.h"
#include "ui_registerui.h"
#include <QPixmap>
#include "chatsocket.h"
#include <QMessageBox>
RegisterUI* RegisterUI::m_pRegister = nullptr;

RegisterUI::RegisterUI(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RegisterUI)
{
    ui->setupUi(this);
    ui->label_icon->setPixmap(QPixmap("res/Icon/joker.png"));
    ui->label_logo->setPixmap(QPixmap("res/img/signatrue.png"));
    setShadow();
}

RegisterUI::~RegisterUI()
{
    delete ui;
}

RegisterUI *RegisterUI::getInstance()
{
    if(m_pRegister == nullptr)
        m_pRegister = new RegisterUI;
    return m_pRegister;
}

void RegisterUI::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton)
    {
        isPressedWidget = true; // 当前鼠标按下的是QWidget而非界面上布局的其它控件
        last = event->globalPos();
    }
}

void RegisterUI::mouseMoveEvent(QMouseEvent *event)
{
    if (isPressedWidget)
    {
        int dx = event->globalX() - last.x();
        int dy = event->globalY() - last.y();
        last = event->globalPos();
        move(x()+dx, y()+dy);
    }
}

void RegisterUI::mouseReleaseEvent(QMouseEvent *event)
{
    int dx = event->globalX() - last.x();
    int dy = event->globalY() - last.y();
    move(x()+dx, y()+dy);
    isPressedWidget = false; // 鼠标松开时，置为false
}


void RegisterUI::setShadow()
{
    //设置无边框
    setWindowFlags(Qt::FramelessWindowHint);
    //设置窗体透明
    setAttribute(Qt::WA_TranslucentBackground, true);
    QGraphicsDropShadowEffect *shadow=new QGraphicsDropShadowEffect();
    shadow->setBlurRadius(15);//设置圆角半径
    shadow->setColor(QColor(0,0,0));//设置边框颜色
    shadow->setOffset(0,0);
    ui->widget->setStyleSheet("background-color:white");
    ui->widget->setGraphicsEffect(shadow);
}

void RegisterUI::on_btn_close_clicked()
{
    m_pRegister = nullptr;
    close();
}

void RegisterUI::on_btn_min_clicked()
{
    hide();
}

void RegisterUI::on_btn_register_clicked()
{
    QString nickname = ui->edit_name->text();
    QString pwd = ui->edit_pwd->text();
    if(nickname==nullptr||pwd==nullptr)
    {
        QMessageBox::information(this,"提示","昵称或密码不能为空");
        return;
    }
    ChatSocket::getInstance()->OnRegister(nickname, pwd);
}
